package com.android.myapplication.view.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.android.myapplication.view.adapter.StudentBean;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MySqlite extends SQLiteOpenHelper {
    private final static int version = 3;

    public MySqlite(@Nullable Context context) {
        super(context, "myDb", null, version);
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table student (" +
                " _id INTEGER PRIMARY KEY AUTOINCREMENT," +
                " name text," +
                " age short, "+
                " sex short, "+
                " img text, "+
                " class_name text "+
                ")");

        db.execSQL("create table class (" +
                " _id INTEGER PRIMARY KEY AUTOINCREMENT," +
                " name text," +
                " year short "+
                ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table student");
        onCreate(db);
    }

    public void saveStudent(StudentBean studentBean){
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        sqLiteDatabase.beginTransaction();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name" ,studentBean.getName());
        contentValues.put("class_name" ,studentBean.getClassName());
        contentValues.put("age" ,studentBean.getAge());
        contentValues.put("sex" ,studentBean.getSex());
        long id = sqLiteDatabase.insert("student", null, contentValues);
        Log.d("AAA", "数据库插入返回 id = " + id);
        sqLiteDatabase.setTransactionSuccessful();
        sqLiteDatabase.endTransaction();
    }


    public void saveClass(ClassBean classBean){
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        sqLiteDatabase.beginTransaction();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name" ,classBean.getName());
        contentValues.put("year" ,classBean.getYear());
        long id = sqLiteDatabase.insert("class", null, contentValues);
        Log.d("AAA", "数据库插入返回 id = " + id);
        sqLiteDatabase.setTransactionSuccessful();
        sqLiteDatabase.endTransaction();
    }

    public void deleteClass(ClassBean classBean){
        SQLiteDatabase sqLiteDatabase = getReadableDatabase();
        sqLiteDatabase.beginTransaction();
        ContentValues contentValues = new ContentValues();
        sqLiteDatabase.delete("class", "where id = ?" , new String[]{String.valueOf(classBean.getId())});
        sqLiteDatabase.setTransactionSuccessful();
        sqLiteDatabase.endTransaction();
    }
    public void updateClass(ClassBean classBean){
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        sqLiteDatabase.beginTransaction();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name" ,classBean.getName());
        contentValues.put("year" ,classBean.getYear());
        long id = sqLiteDatabase.update("class",  contentValues, "where id = ?" , new String[]{String.valueOf(classBean.getId())});
        sqLiteDatabase.setTransactionSuccessful();
        sqLiteDatabase.endTransaction();
    }

}
